//
//  SqliteUseController.m
//  Objc进阶Demo
//
//  Created by Don't update on 2018/7/26.
//  Copyright © 2018年 Don't update. All rights reserved.
//

#import "SqliteUseController.h"
#import "DataBase.h"

@interface SqliteUseController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *list;
@end


@implementation SqliteUseController

-(NSArray *)list{
    if (_list == nil) {
        _list = [[NSArray alloc]initWithObjects:@"创建数据库/打开数据库",@"创建表",@"删除表",@"插入数据",@"查询表",nil];
    }
    return _list;
}

-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        [self list];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  _list.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identfiftysql = @"identifitysql";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfiftysql];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfiftysql];
    }
    cell.textLabel.text = _list[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([_list[indexPath.row] isEqualToString:@"创建数据库/打开数据库"]) {
        [self openDB];
        return;
    }
    if([_list[indexPath.row] isEqualToString:@"创建表"]){
        [self creatTable];
        return;
    }
    if([_list[indexPath.row] isEqualToString:@"删除表"]){
        [self deleteTable];
        return;
    }
    if([_list[indexPath.row] isEqualToString:@"插入数据"]){
        [self InsertTable];
    }
    if([_list[indexPath.row] isEqualToString:@"查询表"]){
        [self selectedTable];
    }
}


#pragma mark ----数据库方法
-(void)openDB{
    [DataBase defaultDB];
}

-(void)creatTable{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"创建表" message:@"输入表名" preferredStyle:(UIAlertControllerStyleAlert)];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"表名";
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *textfield = alertVC.textFields[0];
        [[DataBase defaultDB] createTable:textfield.text];
    }]];
    [self presentViewController:alertVC animated:YES completion:nil];

}

-(void)deleteTable{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"删除表" message:@"输入表名" preferredStyle:(UIAlertControllerStyleAlert)];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"表名";
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleRoundedRect;
    }];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"conform" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *textField = alertVC.textFields[0];
        [[DataBase defaultDB]deleteTableWithName:textField.text];
    }]];
    [self presentViewController:alertVC animated:YES completion:nil];
}



-(void)InsertTable{
    NSArray *colum = [[NSArray alloc]initWithObjects:@"page",@"type",@"message",@"create_time" ,nil];
    NSArray *data = [[NSArray alloc]initWithObjects:@"12",@"'ceshi'",@"'hello'",@"'2018-12-11'", nil];
    
    [[DataBase defaultDB]InsertDataWithTabel:@"Student" WithColums:colum WithData:data];
    
}


-(void)selectedTable{
    UIAlertController *control = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入要查询的表名" preferredStyle:UIAlertControllerStyleAlert];
    [control addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"表名";
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.borderStyle = UITextBorderStyleLine;
    }];
    [control addAction:[UIAlertAction actionWithTitle:@"查询" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *textfield = control.textFields[0];
        [[DataBase defaultDB]selectTableName:textfield.text];
    }]];
    [self presentViewController:control animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView];
}



 

@end
